<?php
	include_once 'isLogin.php';
	$add_success = null;
	$table_name = "companies";
	$table_level_name = "companies_level";
	$table_detail = "companies_detail";
	$table_detail_title = "companies_detail_title";
	$table_categories ="companies_categories";
	
	if($_GET['action']=='submit'){
		//编辑状态先获得原内容
		if($_GET['id']!=false){
			$old = $commonc->getInfo($table_name,$_GET['id'],'img');
			$img = $old['img'];
		}
		//上传图片，生成缩略图
		if($_FILES['img']['name']!=''){
			//设置路径
			//$folder= $GLOBALS['common']['imgPath']."upload/image/";
			$folder="../admin/upload/image/";
			$annexFolder = $folder."large/";
			$middleFolder = $folder."middle/";
			$smallFolder = $folder."small/";
			//删除旧图
			@unlink($annexFolder.$img);
			@unlink($middleFolder.$img);
			@unlink($smallFolder.$img);
			//上传新图
			$upimg = new uploadImg();
			$img = $upimg->upLoad('img',$annexFolder);
			//中图
			$upimg->smallImg($annexFolder.$img,$middleFolder);
			//小图
			$upimg->smallImg($annexFolder.$img,$smallFolder);
		}
		//获取营业执照
		if($_GET['id']!=false){
			$olds = $commonc->getInfo($table_name,$_GET['id'],'businesslicense');
			$businesslicense = $olds['businesslicense'];
		}
		//上传图片，生成缩略图
		if($_FILES['businesslicense']['name']!=''){
			//设置路径
			//$folder= $GLOBALS['common']['imgPath']."upload/image/";
			$folder="../admin/upload/test/";
			$annexFolder = $folder."large/";
			$middleFolder = $folder."middle/";
			$smallFolder = $folder."small/";
			//删除旧图
			@unlink($annexFolder.$businesslicense);
			@unlink($middleFolder.$businesslicense);
			@unlink($smallFolder.$businesslicense);
			//上传新图
			$upimg = new uploadImg();
			$businesslicense = $upimg->upLoad('businesslicense',$annexFolder);
			//中图
			$upimg->smallImg($annexFolder.$businesslicense,$middleFolder);
			//小图
			$upimg->smallImg($annexFolder.$businesslicense,$smallFolder);
		}
		//获取法人照片
		if($_GET['id']!=false){
			$old = $commonc->getInfo($table_name,$_GET['id'],'legalperson');
			$legalperson = $old['legalperson'];
		}
		
		//上传图片，生成缩略图
		if($_FILES['legalperson']['name']!=''){
			//设置路径
			//$folder= $GLOBALS['common']['imgPath']."upload/image/";
			$folder="../admin/upload/person/";
			$annexFolder = $folder."large/";
			$middleFolder = $folder."middle/";
			$smallFolder = $folder."small/";
			//删除旧图
			@unlink($annexFolder.$img);
			@unlink($middleFolder.$img);
			@unlink($smallFolder.$img);
			//上传新图
			$upimg = new uploadImg();
			$legalperson = $upimg->upLoad('legalperson',$annexFolder);
			//中图
			$upimg->smallImg($annexFolder.$legalperson,$middleFolder);
			//小图
			$upimg->smallImg($annexFolder.$legalperson,$smallFolder);
		}
		
		//插入数据库
		//省名称
		
		if(preg_match("/[^\d-., ]/",$_POST['province'])){
          $data = array('province'=>$_POST['province'],'cirty'=>$_POST['city'],'county'=>$_POST['county'],'img'=>$img,'businesslicense'=>$businesslicense,'legalperson'=>$legalperson);
		}else{
			$province=$commonc->getInfo("pcc_province",$_POST['province'],$key="*",$search_key="provinceid",$debug='0');
		$cirty=$commonc->getInfo("pcc_city",$_POST['city'],$key="*",$search_key="cityid",$debug='0');
		$county=$commonc->getInfo("pcc_county",$_POST['county'],$key="*",$search_key="countyid",$debug='0');

		$data = array('province'=>$province['province'],'cirty'=>$cirty['city'],'county'=>$county['county'],'img'=>$img,'businesslicense'=>$businesslicense,'legalperson'=>$legalperson,'state'=>1);
			}
		if($_GET['id']!=false){
			$commonc->update($table_name,$data,"id='".$_GET['id']."'");
			$href = $_basename.'?id='.$_GET['id'];
		}else{
			$commonc->insert($table_name,$data);
			$href='company_list.php';
			$id = $commonc->insert_id();
			$data = array('sort'=>$id);
			$commonc->update($table_name, $data, "id='".$id."'");
		
		}
	echo $commonc->submitAlert('成功',$href);
	}
	if($_POST['content']!='' || $_POST['content_id']!=''){
		foreach($_POST['content'] as $keys=>$vas){
			$array2 = explode('.', $vas);
			//var_dump($array2[0]);
		foreach($_POST['content_id'] as $key=>$va){
			$a[$va]=$_POST['content'][$key];
			}
		}
	}
	if($_GET['action']=='submit'){
		    $title=implode("','",$_POST['content_id']);
			$b=(implode("','",$a));
			//去数据库查询是否存在这个公司的信息
			$res=$commonc->getInfo($table_detail,$_GET['id'],$key="*",$search_key="company_id",$debug='0');
			if($res!='' && $res!=null){
				$ids = implode(',', array_keys($a));
				//var_dump($ids);
				$sql = "UPDATE ".$table_detail."  SET content = CASE title_id ";
				foreach ($a as $id => $ordinal) {
					//var_dump($ordinal);
					$sql.= sprintf("WHEN %d THEN '%s' ", $id, $ordinal);
				}
				$sql .= "END WHERE company_id='".$_GET['id']."' and title_id IN ($ids)";
				mysql_query($sql);
			}else{
				foreach($a as $key=>$valu){
				$data = array('content'=>$valu,'company_id'=>$_GET['id'],'title_id'=>$key);
				//var_dump($data);
				$commonc->insert($table_detail,$data);
				}
				
				if($_POST['content']==""||$_POST['content']==null)
				{
					echo $add_success=1;
					exit();
				};
				
				//$href='company_add.php';
				$id = $commonc->insert_id();
				$data = array('sort'=>$id);
			    $add_success = $commonc->update($table_detail, $data, "id='".$id."'");
			}
		
		echo $add_success;
		exit();
		echo $commonc->submitAlert('成功',$href);
	}
	//获取公司评级数组
	$score = '';
	$name = '';
	$all_level = $commonc->getList($table_level_name);
	if (is_array($all_level['result'])) {
	    foreach ($all_level['result'] as $k=>$v)
	    {
	        $score.= $v['ID'].'|'.$v['score'].',';
	        $name.= $v['ID'].'|'.$v['name'].',';
	      
	    }
	}
	//获取公司行业数组
	$sql=$table_name." c left join ".$table_categories." g on c.upid=g.sort";
	$trade=$commonc->getInfo($sql,$_GET['id'],'g.upid,g.title','c.id');
	$sql1=$table_categories;
	$trades=$commonc->getInfo($sql1,$trade['upid'],'title','id');
	//查询所属的一级分类
	$on = -1;
	if($_GET['id']!=false||$_POST['trade_id']){
	  /*获取公司 名公司评级*/
	    $sql = $table_name.' c left join companies_level cl on c.level_id=cl.ID left join companies_trade ct on c.trade_id=ct.id';
		$row=$commonc->getInfo($sql,$_GET['id'],' c.star, c.id,c.province,c.cirty,c.county,c.img,c.title,cl.score,cl.name,c.businesslicense,c.legalperson,ct.title as trade_name','c.id');
		$smarty->assign('row',$row);
		/*获取公司详情*/
		if($_GET['id'])
		{
			$on = $_GET['id'];
		}
		else
		{
			if($_POST['trade_id'])
		{
			$on = $_POST['trade_id'];
		}
		}
		
	}
	
	$sql = $table_detail_title.' cdt left join companies_detail cd on cdt.id = cd.title_id and cd.company_id= '.$on.' ';
	$sql.= " order by cdt.sort asc ";
    $key = ' cdt.title ,cd.content,cdt.import,cd.id as cd_id,cdt.id';
	
    $list =$commonc->getList($sql,$sql,$key);
	foreach($list['result'] as $key=>$val){
		if($key=="6"){
		  unset($list['result'][$key]);
		}
	}
	//print_r($list);
	//省市区下拉列表
	
	$sql = "pcc_province"; 
	$result = $commonc->getList($sql);  
	$smarty->assign('trades',$trades);
	$smarty->assign('result', $result['result']);
	$smarty->assign('add_success',$add_success);
	$smarty->assign('score', $score);
	$smarty->assign('name', $name);
	$smarty->assign('trade',$trade);
	$smarty->assign("list",$list['result']);
	$smarty->assign('page',$list['page']);
	$smarty->assign('area',$area);
	$smarty->display($_filename.".html");
	
?>